﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;

namespace YetAnotherChat.EntityFrameworkDataAccess
{
    /// <summary>
    /// Base class for all repository, to enalbe to keep DB context within a single
    /// Http request, will be diposed by a DI framework at end of request
    /// </summary>
    public class DisposableRepositoryBase: IDisposable
    {
        protected YetAnotherChatDbContext _context;
        private bool disposed = false;
        protected ILog _logger = null;

        public DisposableRepositoryBase()
        {
            _context = new YetAnotherChatDbContext();
            _logger = LogManager.GetLogger(this.GetType());
        }

        // implements IDisposable
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                // if this is a dispose call dispose on all state you
                // hold, and take yourself off the Finalization queue.
                if (disposing)
                {
                    _context.Dispose();
                }
                this.disposed = true;
            }
        }

        ~DisposableRepositoryBase()
        {
            Dispose(false);
        }
    }
}
